<template>
    <div class="login-wrap">
        <div class="ms-title">后台管理系统</div>
        <div class="ms-login">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="demo-ruleForm">
                <el-form-item prop="username">
                    <el-input v-model="ruleForm.username" placeholder="用户名"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" placeholder="密码" v-model="ruleForm.password" @keyup.enter.native="submitForm('ruleForm')"></el-input>
                </el-form-item>
                <el-form-item prop="verify">
                  <el-row>
                    <el-col :span="12"><el-input type="verify" placeholder="验证码" v-model="ruleForm.verify" @keyup.enter.native="submitForm('ruleForm')"></el-input></el-col>
                    <el-col :span="10" :offset="2">
                      <div class="picbox">
                        <img src="" />
                      </div>
                    </el-col>
                  </el-row>
                </el-form-item>
                <div class="remember">
                  <el-checkbox v-model="ruleForm.checked" @change="onchange">记住密码</el-checkbox>
                </div>
                <div class="login-btn">
                    <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
                </div>
                <!-- <p style="font-size:12px;line-height:30px;color:#FA5555;" v-if="flag">{{err}}</p> -->
            </el-form>
        </div>
    </div>
</template>

<script>
import { login } from '../server/getDate.js'
import { setStore,setCookie,getCookie,delCookie } from '../utils/local.js'
import { mapActions } from 'vuex'
export default {
    data: function () {
        return {
            ruleForm: {
                username: '',
                password: '',
                verify: '',
                checked: false
            },
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' }
                ],
                verify: [
                    { required: false, message: '请输入验证码', trigger: 'blur' }
                ]
            },
            err: '',
            flag: false
        }
    },
    created() {
      this.checkLogin()
    },

    methods: {
        ...mapActions(["saveLogin"]),
        checkLogin(){
          if(getCookie('username') && getCookie('password')){
            this.ruleForm.username = getCookie('username');
            this.ruleForm.password = getCookie('password');
            this.ruleForm.checked = true;
          }
        },
        onchange(){
          if(!this.checked){
            delCookie('username');
            delCookie('password');
          }
        },
        submitForm(formName) {
            const self = this;
            self.$refs[formName].validate((valid) => {
                let body = {}
                body.username = self.ruleForm.username
                body.password = self.ruleForm.password
                if (valid) {
                    login({ body })
                        .then(res => {
                            if (res.status === 0) {
                                setStore('user', res.result)
                                this.saveLogin(res)
                                if(self.ruleForm.checked){
                                  setCookie('username',self.ruleForm.username,7); //保存帐号到cookie，有效期7天
                                  setCookie('password',self.ruleForm.password,7); //保存密码到cookie，有效期7天
                                }
                                self.$router.push({
                                    path: '/home'
                                });
                            } else {
                                this.$message.error(res.message);
                            }
                        },
                        err => { this.$message.error('网络异常') })
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        }
    }
}
</script>

<style scoped>
.login-wrap {
  position: relative;
  width: 100%;
  height: 100%;
}
.ms-title {
  position: absolute;
  top: 50%;
  width: 100%;
  margin-top: -250px;
  text-align: center;
  font-size: 30px;
  color: #fff;
}
.ms-login {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 300px;
  height: auto;
  margin: -170px 0 0 -190px;
  padding: 40px;
  border-radius: 5px;
  background: #fff;
}
.remember {
  margin-bottom: 20px;
}
.picbox {
  background-color: #efecec;
  border-radius: 3px;
  cursor: pointer;
}
.login-btn {
  text-align: center;
}
.login-btn button {
  width: 100%;
  height: 36px;
}
</style>
